//PlatformClearingModel
//2021-01-20 12:47:09

package models

import(
    "git.900sui.cn/kc/base/common/functions"
    "git.900sui.cn/kc/base/common/models/base"
    "git.900sui.cn/kc/kcgin/orm"
	"git.900sui.cn/kc/rpcinterface/interface/pay"
)

//表结构体
type PlatformClearingModel struct {
	Model *base.Model
	Field PlatformClearingModelField
}

//表字段
type PlatformClearingModelField struct{
	T_table	string	`default:"platform_clearing"`
	F_id	string	`default:"id"`
	F_bill_date	string	`default:"bill_date"`
	F_amount	string	`default:"amount"`
	F_status	string	`default:"status"`
	F_agent_type	string	`default:"agent_type"`
	F_agent_id	string	`default:"agent_id"`
	F_create_time	string	`default:"create_time"`
}

//初始化
func (p *PlatformClearingModel) Init(ormer ...orm.Ormer) *PlatformClearingModel{
	functions.ReflectModel(&p.Field)
	p.Model = base.NewModel(p.Field.T_table, ormer...)
	return p
}

//新增数据
func (p *PlatformClearingModel) Insert(data map[string]interface{}) (int){
	result,_ := p.Model.Data(data).Insert()
	return result
}

func (p *PlatformClearingModel) GetByBilldate ( billDate string ) map[string]interface{} {
	if len(billDate) == 0{
		return map[string]interface{}{}
	}

	return  p.Model.Where([]base.WhereItem{
		{
			Field: p.Field.F_bill_date,
			Value: billDate,
		},
	}).Limit(0,1).Find()
}

func (p *PlatformClearingModel) UpdateById(id int, data map[string]interface{}) bool  {
	if id <= 0 || len(data) == 0{
		return false
	}

	_, err := p.Model.Where([]base.WhereItem{
		{
			Field: p.Field.F_id,
			Value: id,
		},
	}).Data(data).Update()

	if err != nil{
		return false
	}

	return true
}

// 根据id和status更新
func (p *PlatformClearingModel) UpdateByIdAndStatus(id, status interface{}, data map[string]interface{}) (int, error) {
	return p.Model.Where(map[string]interface{}{
		p.Field.F_id:     id,
		p.Field.F_status: status,
	}).Data(data).Update()
}


// 根据状态获取分页数据
func (p *PlatformClearingModel) Lists(billDate string, start, limit int) []map[string]interface{} {
	return p.Model.Where(map[string]interface{}{
		p.Field.F_status:    pay.CLEAR_STATUS_none,
		p.Field.F_bill_date: []interface{}{"elt", billDate},
	}).Limit(start, limit).OrderBy(p.Field.F_id).Select()
}



// 根据agentId，agentType，status更新数据
func (p *PlatformClearingModel) UpdateAgent(agentId, agentType, status interface{}, data map[string]interface{}) (int, error) {
	maps := []base.WhereItem{
		{p.Field.F_agent_id,agentId},
		{p.Field.F_agent_type,agentType},
		{p.Field.F_status,status},
	}
	return p.Model.Where(maps).Data(data).Update()
}